package cn.itsub.sodo.view;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.RenderingHints;
import java.util.Locale;

import javax.swing.JFrame;
import javax.swing.JPanel;

import cn.itsub.sodo.core.Config;
import cn.itsub.sodo.core.Director;
import cn.itsub.sodo.core.types.Size;
import cn.itsub.sodo.core.types.Vec2;


public class SwingView extends HiView{

	@Override
	public HiView init(Size frameSize, Size designSize) {
		this.frameSize = frameSize;
		this.designSize = designSize;
		final JFrame jframe = new JFrame();
		this.frame = jframe;
		jframe.setSize((int)frameSize.width,(int)frameSize.height);
		jframe.setLocationRelativeTo(null);
		jframe.setBackground(Color.black);
		jframe.setResizable(false);
		jframe.setVisible(true);
		jframe.setVisible(false);
		Insets set=frame.getInsets();
		jframe.setSize((int)frameSize.width+set.left+set.right, (int)frameSize.height+set.top+set.bottom);
		jframe.add(new MyPanel(this));
		jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jframe.setVisible(true);
		//jframe.enableInputMethods(false);
		frame.setLocale(Locale.ENGLISH);
		return this;
	}
	
	public static SwingView create(Size frameSize,Size designSize){
		SwingView view = new SwingView();
		view.init(frameSize, designSize);
		//view.start(1.0f/80);
		return view;
	}
	
}
class MyPanel extends JPanel{

	private HiView view = null;
	public MyPanel(HiView view) {
		this.view = view;
		this.view.canvas = this;
	}
	@Override
	public void paint(Graphics gr) {
		Graphics2D g = (Graphics2D) gr;
		Director director = Director.getInstance();

		//g2.fillRect(0, 0, this.getWidth(), this.getHeight());

		float scaleX = this.getWidth()/view.designSize.width;
		float scaleY = this.getHeight()/view.designSize.height;
		g.scale(scaleX, scaleY);
		
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
		
		
		if(director.runningScene != null)
			director.runningScene.visit(g);
		if( director.displayFPS ){
			g.setFont(Config.FPS_FONT);
			g.setColor(Config.FPS_COLOR);
			g.drawString(String.format("FPS:%.1f", director.getFPS()), 50, 50);
		}
			
		
		//g2.drawLine(0, 0, 960, 540);
	}
	
}